import Taro from "@tarojs/taro";
 
// api请求封装
const htttRequest = function (url: string, paramet: object, method: any, showToast: Boolean) {
 
  // 获取token
  const info:any  = Taro.getStorageSync('info')
  const token:any = info?JSON.parse(info).token:''
 
  if (showToast) {
    Taro.showLoading({
      title: '加载中',
    })
  }
 
  return new Promise<{}>((resolve, reject) => {
    Taro.request({
      url: APISRC + url,
      data: paramet,
      method: method,
      header: {
        'content-type': 'application/json',
        'Authorization': info ? `Bearer ${token}` : '',
      }
    }).then((res) => {
      Taro.hideLoading()
      switch (res.data.code) {
        case 200:
          return resolve(res.data)
        default:
            Taro.showToast({
              title: res.data.msg,
              icon: 'none',
            })
 
      }
    }).catch(err => {
      Taro.showToast({
        title: '小程序数据请求失败',
        icon: 'none'
      })
      return reject(err)
    })
  })
}
 
export default htttRequest
